<template>
  <div class="login-container">
    <div class="cards-wrapper">
      <!-- 介绍卡片 -->
      <div class="intro-card">
        <div class="intro-header">
          <h2>🎬 智能电影推荐</h2>
        </div>
        <div class="intro-content">
          <div class="feature-item">
            <div class="feature-icon">🎯</div>
            <div class="feature-text">
              <h3>个性化推荐</h3>
              <p>基于您的观影历史和评分，为您推荐最符合口味的电影</p>
            </div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">⭐</div>
            <div class="feature-text">
              <h3>智能评分</h3>
              <p>多维度评分系统，帮助您发现高质量的优秀电影</p>
            </div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">💬</div>
            <div class="feature-text">
              <h3>社区互动</h3>
              <p>与其他影迷分享观影心得，交流电影见解</p>
            </div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">🔍</div>
            <div class="feature-text">
              <h3>智能搜索</h3>
              <p>强大的搜索功能，快速找到您想要的电影</p>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 登录卡片 -->
      <div class="login-card">
        <div class="login-header">
          <h1>电影推荐系统</h1>
          <p>登录您的账户</p>
        </div>
        
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
              id="username"
              v-model="form.username"
              type="text"
              required
              :disabled="authStore.isLoading"
              placeholder="请输入用户名"
            />
          </div>
          
          <div class="form-group">
            <label for="password">密码</label>
            <input
              id="password"
              v-model="form.password"
              type="password"
              required
              :disabled="authStore.isLoading"
              placeholder="请输入密码"
            />
          </div>
          
          <div v-if="authStore.error" class="error-message">
            {{ authStore.error }}
          </div>
          
          <button
            type="submit"
            class="login-button"
            :disabled="authStore.isLoading"
          >
            <span v-if="authStore.isLoading">登录中...</span>
            <span v-else>登录</span>
          </button>
        </form>
        
        <div class="login-footer">
          <p>
            还没有账户？
            <router-link to="/register" class="register-link">
              立即注册
            </router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 表单数据
const form = ref({
  username: '',
  password: '',
})

// 处理登录
const handleLogin = async () => {
  try {
    await authStore.login(form.value)
    
    // 检查用户是否为管理员，如果是则跳转到admin页面
    if (authStore.user?.is_staff) {
      router.push({ name: 'Admin' })
    } else {
      router.push({ name: 'home' })
    }
  } catch (error) {
    console.error('登录失败:', error)
  }
}

// 清除错误信息
onMounted(() => {
  authStore.clearError()
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.cards-wrapper {
  display: flex;
  gap: 0;
  max-width: 900px;
  width: 100%;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  overflow: hidden;
}

.intro-card {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  padding: 40px;
  flex: 1;
  min-width: 400px;
}

.intro-header {
  text-align: center;
  margin-bottom: 30px;
}

.intro-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.intro-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.feature-icon {
  font-size: 24px;
  flex-shrink: 0;
  margin-top: 4px;
}

.feature-text h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
}

.feature-text p {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  opacity: 0.9;
}

.login-card {
  background: white;
  padding: 40px;
  flex: 1;
  min-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  margin: 0;
  font-size: 16px;
}

.login-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
}

.form-group input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.error-message {
  background-color: #fee;
  color: #c33;
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 14px;
  border: 1px solid #fcc;
}

.login-button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.login-footer {
  text-align: center;
}

.login-footer p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.register-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
}

.register-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cards-wrapper {
    flex-direction: column;
    max-width: 400px;
  }
  
  .intro-card,
  .login-card {
    min-width: auto;
  }
  
  .intro-card {
    border-radius: 12px 12px 0 0;
  }
  
  .login-card {
    border-radius: 0 0 12px 12px;
  }
}

@media (min-width: 769px) {
  .intro-card {
    border-radius: 12px 0 0 12px;
  }
  
  .login-card {
    border-radius: 0 12px 12px 0;
  }
}
</style>